import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;

public class SuperHeater extends Script implements PaintListener {

	public int ore1;
	public int ore2;
	public int amount1;
	public int amount2;
	public int need1;
	public int need2;
	public int bar;
	public int nat = 561;
	public int[] bankid;
	public int bankperson;
	public int bothores;

	public long time1;
	public long time2;
	public long time3;
	public int timeset;
	public long startTime = System.currentTimeMillis();
	private String status1 = "";
	private String status2 = "";
	private String status3 = "";
	private String status4 = "";
	public String stat = "";
	private String color;

	int magexp;
	int smithxp;
	int magexp1;
	int smithxp1;

	public boolean cast() {
		openTab(Constants.TAB_MAGIC);
		clickMouse(670 + random(-5, 5), 300 + random(-5, 5), true);
		wait(100);
		atInventoryItem(ore1, "Cast ");
		wait(200);
		return true;
	}

	public boolean checkBank() {
		if (bank.isOpen()) {
			return true;
		}
		return false;
	}

	public boolean doBank() {
		if (bank.isOpen()) {
			if (inventoryContains(bar)) {
				bank.depositAllExcept(nat);
				wait(500);
				bank.depositAllExcept(nat);
				wait(500);
			}
			if (bank.getCount(ore1) >= amount1) {
				if (bank.getCount(ore2) >= amount2) {
					if (getInventoryCount(ore1) == 0) {
						withdraw2(ore1, amount1);
					}
					if (getInventoryCount(ore1) > amount1) {
						bank.depositAllExcept(nat);
					}
					if (bothores == 1) {
						if (getInventoryCount(ore2) == 0) {
							withdraw2(ore2, amount2);
						}
						if (getInventoryCount(ore2) > amount2) {
							bank.depositAllExcept(nat);
						}
					}
					bank.close();
					return true;
				}
				log("No more ores");
				bank.close();
				stopAllScripts();
				return false;
			}
			log("No more ores");
			bank.close();
			stopAllScripts();
			return false;
		}
		return false;
	}

	public String getAuthor() {
		return "Someone";
	}

	public String getName() {
		return "SuperHeater";
	}

	public String getScriptCategory() {
		return "Magic";
	}

	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"6\" color=\"red\">" + getName() + " v"
				+ getVersion() + " c" + "</font></b><br>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">";
		html += "<tr><td><b>Options:</b></td>";
		html += "<strong>Which Location?</strong>";
		html += "<br />";
		html += "<select name=\"Bank\">";
		html += "<option>Grand Exchange";
		html += "</select>";
		html += "<br />";
		html += "<strong>Which Bar?</strong>";
		html += "<br />";
		html += "<select name=\"Bar\">";
		html += "<option>Bronze";
		html += "<option>Iron";
		html += "<option>Silver";
		html += "<option>Steel";
		html += "<option>Gold";
		html += "<option>Mith";
		html += "<option>Addy";
		html += "<option>Rune";
		html += "</select>";
		html += "<br />";
		html += "<strong>What color for paint?</strong>";
		html += "<br />";
		html += "<select name=\"SelectedColor\">";
		html += "<option selected>WHITE";
		html += "<option>BLACK";
		html += "<option>PINK";
		html += "<option>RED";
		html += "<option>CYAN";
		html += "<option>GREEN";
		html += "<option>YELLOW";
		html += "</select>";
		html += "<br />";
		html += "<tr><td colspan=2><b><font size=\"3\">&nbsp&nbsp Good luck.</font></b></td><td  colspan=\"2\" align=right>";
		html += "</center></body>";
		html += "</html>";
		return html;
	}

	public double getVersion() {
		return 1.2;
	}

	public int loop() {
		if (needBank()) {
			status("Need to Bank");
			if (checkBank()) {
				doBank();
				status("Banking");
				return random(200, 400);
			}
			openBank();
			status("Opening Bank");
			return random(500, 600);
		}
		cast();
		status("Casting");
		return random(300, 500);
	}

	public boolean needBank() {
		if (getInventoryCount(ore1) >= need1) {
			if (getInventoryCount(ore2) >= need2) {
				return false;
			}
			return true;
		}
		return true;
	}

	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
		return;
	}

	public void onRepaint(final Graphics render) {
		int mageGained = 0;
		int smithGained = 0;
		long mageHour;
		long smithHour;
		final int mgp = skills.getPercentToNextLevel(Constants.STAT_MAGIC);
		final int smp = skills.getPercentToNextLevel(Constants.STAT_SMITHING);

		if (magexp == 0) {
			magexp = skills.getCurrentSkillExp(Constants.STAT_MAGIC);
		}
		if (smithxp == 0) {
			smithxp = skills.getCurrentSkillExp(Constants.STAT_SMITHING);
		}
		magexp1 = skills.getCurrentSkillExp(Constants.STAT_MAGIC);
		smithxp1 = skills.getCurrentSkillExp(Constants.STAT_SMITHING);
		mageGained = magexp1 - magexp;
		smithGained = smithxp1 - smithxp;

		long millis = System.currentTimeMillis() - startTime;
		final long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		final long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		final long seconds = millis / 1000;
		final long minutes2 = minutes + hours * 60;

		if (minutes == 0) {
			mageHour = 1337;
			smithHour = 1337;
		} else {
			mageHour = mageGained / minutes2 * 60;
			smithHour = smithGained / minutes2 * 60;
		}

		render.setColor(new Color(0, 0, 0, 50));
		render.fill3DRect(370, 20, 130, 20, true);

		render.setColor(new Color(13, 45, 22, 50));
		render.fill3DRect(370, 50, 130, 55, true);

		render.setColor(new Color(80, 0, 0, 50));
		render.fill3DRect(370, 115, 130, 55, true);

		render.setColor(new Color(0, 0, 0, 50));
		render.fill3DRect(370, 180, 130, 75, true);

		render.setColor(new Color(0, 0, 110, 100));
		render.fill3DRect(380, 85, mgp, 12, true);

		render.setColor(new Color(81, 81, 81, 100));
		render.fill3DRect(380, 150, smp, 12, true);

		final int x = 380;
		final int y = 50;

		if (color == "WHITE") {
			render.setColor(Color.WHITE);
		}
		if (color == "BLACK") {
			render.setColor(Color.BLACK);
		}
		if (color == "PINK") {
			render.setColor(Color.PINK);
		}
		if (color == "RED") {
			render.setColor(Color.RED);
		}
		if (color == "CYAN") {
			render.setColor(Color.CYAN);
		}
		if (color == "GREEN") {
			render.setColor(Color.GREEN);
		}
		if (color == "YELLOW") {
			render.setColor(Color.YELLOW);
		}

		render.drawString("Time running: " + hours + ":" + minutes + ":"
				+ seconds + ".", x, y - 15);
		render.drawString("Mg Gained: " + mageGained, x, y + 15);
		render.drawString("Mg per Hour:  " + mageHour, x, y + 30);
		render.drawString(mgp + "%", x + 50, y + 45);
		render.drawString("Sm Gained: " + smithGained, x, y + 80);
		render.drawString("Sm per Hour:  " + smithHour, x, y + 95);
		render.drawString(smp + "%", x + 50, y + 110);
		render.drawString("Status : " + status1, x, y + 150);
		render.drawString(status2, x, y + 165);
		render.drawString(status3, x, y + 180);
		render.drawString(status4, x, y + 195);

	}

	public boolean onStart(final Map<String, String> args) {

		startTime = System.currentTimeMillis();

		if (args.get("SelectedColor").equals("WHITE")) {
			color = "WHITE";
		}

		if (args.get("SelectedColor").equals("BLACK")) {
			color = "BLACK";
		}

		if (args.get("SelectedColor").equals("PINK")) {
			color = "PINK";
		}

		if (args.get("SelectedColor").equals("RED")) {
			color = "RED";
		}

		if (args.get("SelectedColor").equals("CYAN")) {
			color = "CYAN";
		}

		if (args.get("SelectedColor").equals("GREEN")) {
			color = "GREEN";
		}

		if (args.get("SelectedColor").equals("YELLOW")) {
			color = "YELLOW";
		}

		if (args.get("Bank").equals("Grand Exchange")) {
			bankid = new int[] { 6532, 6533, 6534, 6535 };
			bankperson = 1;
		}

		if (args.get("Bar").equals("Bronze")) {
			ore1 = 436;
			ore2 = 438;
			amount1 = 13;
			amount2 = 13;
			need1 = 1;
			need2 = 1;
			bar = 2349;
			bothores = 1;
		}

		if (args.get("Bar").equals("Steel")) {
			ore1 = 440;
			ore2 = 453;
			amount1 = 9;
			amount2 = 18;
			need1 = 1;
			need2 = 2;
			bar = 2353;
			bothores = 1;
		}

		if (args.get("Bar").equals("Mith")) {
			ore1 = 447;
			ore2 = 453;
			amount1 = 5;
			amount2 = 20;
			need1 = 1;
			need2 = 4;
			bar = 2359;
			bothores = 1;
		}

		if (args.get("Bar").equals("Addy")) {
			ore1 = 449;
			ore2 = 453;
			amount1 = 3;
			amount2 = 18;
			need1 = 1;
			need2 = 6;
			bar = 2361;
			bothores = 1;
		}

		if (args.get("Bar").equals("Rune")) {
			ore1 = 451;
			ore2 = 453;
			amount1 = 3;
			amount2 = 24;
			need1 = 1;
			need2 = 8;
			bar = 2363;
			bothores = 1;
		}

		if (args.get("Bar").equals("Gold")) {
			ore1 = 444;
			amount1 = 27;
			need1 = 1;
			bar = 2357;
		}

		if (args.get("Bar").equals("Silver")) {
			ore1 = 442;
			amount1 = 27;
			need1 = 1;
			bar = 2355;
		}

		if (args.get("Bar").equals("Iron")) {
			ore1 = 440;
			amount1 = 27;
			need1 = 1;
			bar = 2351;
		}
		return true;
	}

	public boolean openBank() {
		if (bankperson == 0) {
			final RSObject bank1 = findObject(2, bankid);
			atObject(bank1, "Use-quickly ");
			return true;
		}
		if (bankperson == 1) {
			final RSNPC bank1 = getNearestNPCByID(bankid);
			atNPC(bank1, "Bank ");
			return true;
		}
		return false;
	}

	public boolean status(final String stat1) {
		if (stat1 == stat) {
			if (timeset == 1) {
				time2 = System.currentTimeMillis();
				time3 = time1 - time2;
				if (time3 > 29999) {
					status4 = status3;
					status3 = status2;
					status2 = status1;
					status1 = stat1;
					timeset = 0;
					return true;
				}
			}
			time1 = System.currentTimeMillis();
			timeset = 1;
			return true;
		}
		status4 = status3;
		status3 = status2;
		status2 = status1;
		status1 = stat1;
		stat = stat1;
		return true;
	}

	public boolean withdraw2(final int ItemID, final int amount) {
		bank.atItem(ItemID, "Withdraw-X ");
		wait(1000);
		Bot.getInputManager().sendKeys("" + amount, true);
		wait(300);
		return true;
	}

}